package com.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Url {
	
	private static final String RE_TOP = "(\\w*\\.?){1}\\.(com.cn|net.cn|gov.cn|org\\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$";
	
	/**
	 * 获得一级域名
	 * @author HZP
	 * @url 传入域名地址
	 * */
	public static String get_top_domain(String url) {
		try {
			URL Url = new URL(url);
			url = Url.getHost();
			Pattern p = Pattern.compile(RE_TOP);  
			Matcher m = p.matcher(url); 
			String domain = "";
			//获取一级域名
			while(m.find()){
				domain = m.group();
			}
			return domain;
		} catch (MalformedURLException e) {
			return "";
		}
	}
}
